package model.comparisons;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import model.IComparison;

public class Comparison4 implements IComparison {

	private List<String> arraylist = new ArrayList<String>();
	private List<String> linkedlist = new LinkedList<String>();
	private boolean isactive;
	private float[] times = new float[2];
	private int N;
	static final String AB = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

	String randomString() {
		Random rnd = new Random();
		StringBuilder sb = new StringBuilder(6);
		for (int i = 0; i < 6; i++)
			sb.append(AB.charAt(rnd.nextInt(AB.length())));
		return sb.toString();
	}

	@Override
	public void setN(int N) {
		this.N = N;

	}

	@Override
	public void setActive(boolean isactive) {
		this.isactive = isactive;
	}

	@Override
	public float[] getTime() {
		if (isactive) {

			for (int i = 0; i < N; i++) {
				arraylist.add(i, randomString());
			}

			float startTime = System.nanoTime();
			Collections.sort(arraylist);

			times[0] = ((float) (System.nanoTime() - startTime)) / 1000000;

			for (int i = 0; i < N; i++) {
				linkedlist.add(i, randomString());
			}

			float startTime2 = System.nanoTime();

			Collections.sort(linkedlist);

			times[1] = ((float) (System.nanoTime() - startTime2)) / 1000000;

			// System.out.println(times[0] + " " + times[1]);// test
			return times;
		}

		return new float[] { 1, 1 };
	}

}
